package com.biovr.backend.restcontroller;

import com.biovr.backend.domain.*;
import com.biovr.backend.repository.*;
import com.biovr.backend.utils.ResponseFactory;
import com.biovr.backend.utils.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by monodev on 2017/6/19.
 */
@Api(value = "/schools",description = "学校信息API")
@RestController()
@RequestMapping("/schools")
public class SchoolRestController  {
    @Autowired
    SchoolRepository schoolRepository;
    @Autowired
    MajorRepository majorRepository;
    @Autowired
    KlassRepository klassRepository;
    @Autowired
    GradeRepository gradeRepository;
    @Autowired
    DepartmentRepository departmentRepository;
    @Autowired
    ExpirementRepository expirementRepository;

    @ApiOperation(value="获得所所有学校信息", notes="返回学校列表",response = School.class,responseContainer = "List")
  @GetMapping
    RestResponse<List<School>> getAll(){
        List<School> schools = schoolRepository.findAll();
        return ResponseFactory.createSuccessResult(schools) ;
    }
    @ApiOperation(value="获得单条学校信息", notes="返回学校对象",response = School.class)
    @ApiImplicitParam(name = "id", value = "学校id", required = true, dataType = "String",paramType = "path")
    @GetMapping("/{id}")
    RestResponse<?> getOne(@PathVariable String id){
       School school = schoolRepository.findOne(id);
        return ResponseFactory.createSuccessResult(school) ;
    }

    @ApiOperation(value="新增/修改学校信息", notes="使用json格式POST添加/修改学校",response = School.class,responseContainer = "List")
    @ApiImplicitParam(name = "school", value = "至少包括学校名称", required = true, dataType = "School")
    @PostMapping
    RestResponse<?> addUpdate(@RequestBody School school) throws Exception{
        //检查名称
        if(null==school.getName()||school.getName()==""){
                return ResponseFactory.createErrorResult("学校名称不能为空");
        }
       School updatedSchool =  schoolRepository.save(school);
        return  ResponseFactory.createSuccessResult(updatedSchool);
    }
    @ApiOperation(value="删除学校信息", notes="删除学校",response =Boolean.class)
    @ApiImplicitParam(name = "id", value = "学校id", required = true, dataType = "String",paramType = "path")
    @DeleteMapping("/id")
    RestResponse<?> delete(@PathVariable String id) throws Exception{
        schoolRepository.delete(id);
        return  ResponseFactory.createSuccessResult();
    }

  /**
   * 获得所有major
   * @return
   */
  @GetMapping("/majors")
  RestResponse<List<Major>> getMajors( ){
    return  ResponseFactory.createSuccessResult(majorRepository.findAll());
  }

  /**
   * 获得学校下的所有major
   * @param schoolId
   * @return
   */
    @GetMapping("/majors/{schoolId}")
    RestResponse<List<Major>> getMajorsOfSchool(@PathVariable String schoolId){
        return  ResponseFactory.createSuccessResult(majorRepository.findBySchoolId(schoolId));
    }
    @PostMapping("/majors/{schoolId}")
    RestResponse<Major> addMajor(@RequestBody Major major) throws Exception{
        return  ResponseFactory.createSuccessResult(majorRepository.save(major));
    }
    @DeleteMapping("/majors/{id}")
    RestResponse<?> delMajor(@PathVariable String id) throws Exception{
        majorRepository.delete(id);
        return  ResponseFactory.createSuccessResult();
    }




  /**
   * 获得所有grade
   * @return
   */
  @GetMapping("/grades")
  RestResponse<List<Grade>> getGrades( ){
    return  ResponseFactory.createSuccessResult(gradeRepository.findAll());
  }

  /**
   * 获得学校下的所有年级
   * @param schoolId
   * @return
   */

    @GetMapping("/grades/{schoolId}")
    RestResponse<List<Grade>> getGradesOfSchool(@PathVariable String schoolId){
        return  ResponseFactory.createSuccessResult(gradeRepository.findBySchoolId(schoolId));
    }
    @PostMapping("/grades/{schoolId}")
    RestResponse<Grade> addGrade(@RequestBody Grade grade) throws Exception{
        return  ResponseFactory.createSuccessResult(gradeRepository.save(grade));
    }
    @DeleteMapping("/grades/{id}")
    RestResponse<?> delGrade(@PathVariable String id) throws Exception{
        //检查年级下有无班级
        List<Klass> klasses = klassRepository.findByGradeId(id);
        if(klasses.size()!=0){
            return ResponseFactory.createErrorResult("年级下尚有班级，无法删除");
        }
        gradeRepository.delete(id);
        return  ResponseFactory.createSuccessResult();
    }


  /**
   * 获得所有class
   * @return
   */
  @GetMapping("/klasses")
  RestResponse<List<Klass>> getKlasses( ){
    return  ResponseFactory.createSuccessResult(klassRepository.findAll());
  }

  /**
   * 获得年级下的所有班级
   * @param gradeId
   * @return
   */
    @GetMapping("/klasses/{gradeId}")
    RestResponse<List<Klass>> getKlassesOfSchool(@PathVariable String gradeId){
        return  ResponseFactory.createSuccessResult(klassRepository.findByGradeId(gradeId));
    }
    @PostMapping("/klasses/{gradeId}")
    RestResponse<Klass> addGrade(@RequestBody Klass klass) throws Exception{
        return  ResponseFactory.createSuccessResult(klassRepository.save(klass));
    }
    @DeleteMapping("/klasses/{id}")
    RestResponse<?> delKlass(@PathVariable String id) throws Exception{
        klassRepository.delete(id);
        return  ResponseFactory.createSuccessResult();
    }

    /**
     * 获得学校下的所有部门
     * @param schoolId
     * @return
     */
    @GetMapping("/departments/{schoolId}")
    RestResponse<List<Department>> getDeptsOfSchool(@PathVariable String schoolId){
        return  ResponseFactory.createSuccessResult(departmentRepository.findBySchoolId(schoolId));
    }
    @PostMapping("/departments/{schoolId}")
    RestResponse<Department> addDept(@RequestBody Department department) throws Exception{
        return  ResponseFactory.createSuccessResult(departmentRepository.save(department));
    }
    @DeleteMapping("/departments/{id}")
    RestResponse<?> delDept(@PathVariable String id) throws Exception{
        departmentRepository.delete(id);
        return  ResponseFactory.createSuccessResult();
    }

  /**
   * 获得授权的实验列表
   * @param schoolId
   * @return
   */
    @GetMapping("/authrizedexpms/{schoolId}")
    RestResponse<List<Expirement>> getAuthrizedExpmsOfSchool(@PathVariable String schoolId){
      School tarSchool = schoolRepository.findOne(schoolId);
      if(tarSchool!=null && tarSchool.getExpmIds()!=null && tarSchool.getExpmIds().size()>0){
          List<Expirement> authedExpms = new ArrayList<Expirement>();
        for (String expmId:tarSchool.getExpmIds()) {
          authedExpms.add(expirementRepository.findOne(expmId));
        }
        return ResponseFactory.createSuccessResult(authedExpms);
      }
      return ResponseFactory.createErrorResult("not found");
    }

}
